


//
//  HMTabBarViewController.m
//  黑马微博-wzy
//
//  Created by Mac on 2016/11/23.
//  Copyright © 2016年 Mac. All rights reserved.
//

#import "HMTabBarViewController.h"
#import "HMNavigationController.h"
//首页
#import "HMHomeTableViewController.h"
//消息
#import "HMMessageViewController.h"
//发现
#import "HMDiscoverViewController.h"
//我
#import "HMProfileViewController.h"

#import "HMTabBar.h"
//发微博
#import "HMComposeViewController.h"
#import "HMUserTool.h"
#import "HMAccountTool.h"
#import "HMAccount.h"


@interface HMTabBarViewController ()<HMTabBarDelegate, UITabBarControllerDelegate>
//首页 控制器
@property (nonatomic, weak) HMHomeTableViewController *home;
//消息 控制器
@property (nonatomic, weak) HMMessageViewController *message;
//我的 控制器
@property (nonatomic, weak) HMProfileViewController *profile;
//最后一个选中的控制器
@property (nonatomic, weak) UIViewController *lastSelectedViewController;


@end

@implementation HMTabBarViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.delegate = self;

    //添加子控制器
    [self addAllChildVCs];

    //创建自定义 tabbar
    [self addCustomTabBar];
    
    
    //使用定时器获取用户未读消息数(做定时器的时候)
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(getUnreadCount) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

}

//点击下方 tabBarItem  细节
//1.如果当前所在控制器就是"首页",则再点击"首页" ->则刷新首页数据,并且滚动到视图最上面
//2.如果当前所在控制器不是"首页",则再点击"首页" ->则不做任何处理(例如当用户在首页浏览到第120行的时候,点击"消息"->跳转到"消息"控制器,当用户再次点击"首页"->返回到"首页"的时候,依然在第120行,优化用户体验)
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UINavigationController *)viewController{
    UIViewController *vc = [viewController.viewControllers firstObject];
    if ([vc isKindOfClass:[HMHomeTableViewController class]]) {
        //如果是首页控制器
        if (self.lastSelectedViewController == vc) {
            //如果最后选中的控制器是"首页",即第一种情况,当前在"首页",再点击"首页"
            //刷新"首页"数据
            [self.home refresh:YES];
        }else{
            [self.home refresh:NO];
        }
    }

    self.lastSelectedViewController = vc;
}


#pragma mark - 获取用户未读消息数
- (void)getUnreadCount{
    //1.封装请求参数
    HMUnreadCountParam *param = [HMUnreadCountParam param];
    param.uid = [HMAccountTool account].uid;
    //2.获取未读数
    [HMUserTool unreadCountWithParams:param success:^(HMUnreadCountResult *result) {
        //成功
        
        //1.显示微博未读数
        if (result.status == 0) {
            //注意当 未读数为0 的时候,应当清空 badgeValue
            self.home.tabBarItem.badgeValue = nil;
        }else{
            //不为0 的时候应当显示
            self.home.tabBarItem.badgeValue = [NSString stringWithFormat:@"%d", result.status];
        
        }
        
        //2.显示消息未读数
        if (result.messageCount == 0) {
            //注意当 未读数为0 的时候,应当清空 badgeValue
            self.message.tabBarItem.badgeValue = nil;
        }else{
            //不为0 的时候应当显示
            self.message.tabBarItem.badgeValue = [NSString stringWithFormat:@"%d", result.messageCount];
            
        }

        //3.显示新粉丝数
        if (result.follower == 0) {
            //注意当 未读数为0 的时候,应当清空 badgeValue
            self.profile.tabBarItem.badgeValue = nil;
        }else{
            //不为0 的时候应当显示
            self.profile.tabBarItem.badgeValue = [NSString stringWithFormat:@"%d", result.follower];
            
        }
        
        //4.在应用图标上显示所有未读数
        [UIApplication sharedApplication].applicationIconBadgeNumber = result.totalCount;
        
        HMLog(@"总未读数 == %d", result.totalCount);
        
    } failure:^(NSError *error) {
        //失败
        HMLog(@"获取未读数失败 === %@", error);
    }];

}


#pragma mark ******** 添加子控制器 *********
- (void)addAllChildVCs{

    HMHomeTableViewController *home = [[HMHomeTableViewController alloc] init];
    [self addOneChildVC:home title:@"首页" imageName:@"tabbar_home" selectedImageName:@"tabbar_home_seleted"];
    self.home = home;
    self.lastSelectedViewController = home;
    
    HMMessageViewController *message = [[HMMessageViewController alloc] init];
    [self addOneChildVC:message title: @"消息" imageName:@"tabbar_message_center" selectedImageName:@"tabbar_message_center_seleted"];
    self.message = message;
    
    HMDiscoverViewController *discover = [[HMDiscoverViewController alloc] init];
    [self addOneChildVC:discover title:@"发现" imageName:@"tabbar_discover" selectedImageName:@"tabbar_discover_seleted"];

    
    HMProfileViewController *profile = [[HMProfileViewController alloc] init];
    [self addOneChildVC:profile title:@"我" imageName:@"tabbar_profile" selectedImageName:@"tabbar_profile_seleted"];
    self.profile = profile;
}

#pragma mark ====== 创建自定义 tabbar =======
- (void)addCustomTabBar{

    HMTabBar *customTabBar = [[HMTabBar alloc] init];
    customTabBar.tabBarDelegate = self;
    //更换tabBar
    [self setValue:customTabBar forKeyPath:@"tabBar"];
}


//创建子控制器
- (void)addOneChildVC:(UIViewController *)childVC title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName{

    //2/设置标题//childVC.title 相当于同时设置tabbar title 和 navgiationBar title
    childVC.title = title;
    //3/设置 图片
    childVC.tabBarItem.image = [UIImage imageNamed:imageName];
    //4/设置 选中时 图片
    UIImage *seletedImage = [UIImage imageNamed:selectedImageName];
//    if (iOS7) {
        //声明这张图片别渲染
        seletedImage =[seletedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//    }
    childVC.tabBarItem.selectedImage = seletedImage;
    //5/添加tabbar控制器的子控制器
    HMNavigationController *nav = [[HMNavigationController alloc] initWithRootViewController:childVC];
    
    [self addChildViewController:nav];

}

#pragma mark - HMTabBarDelegate
- (void)tabBarDelegateClickedPlusButton:(HMTabBar *)tabBar{
    //弹出发微博的控制器
    HMComposeViewController *compose = [[HMComposeViewController alloc] init];
    HMNavigationController *nav = [[HMNavigationController alloc] initWithRootViewController:compose];
    
    [self presentViewController:nav animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
